package com.lisx.module_matters;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.fenghuajueli.libbasecoreui.adapter.DefaultViewPagerAdapter;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.module_route.MattersModuleRoute;
import com.ijianji.lsx_ui_lib.TabLayout;
import com.lisx.module_matters.databinding.ActivityMattersBinding;
import com.lisx.module_matters.dialogfragment.AddMattersDialogFragment;
import com.lisx.module_matters.dialogfragment.AddMattersNewDialogFragment;
import com.lisx.module_matters.fragment.MattersItemFragment;

import java.util.ArrayList;
import java.util.List;

@Route(path = MattersModuleRoute.MATTERS_ACTIVITY)
public class MattersActivity extends BaseActivity {

    private int currentPosition = 0;
    private List<Fragment> fragmentList;
    private List<String> titleList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMattersBinding binding = ActivityMattersBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivFanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        binding.ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddMattersNewDialogFragment fragment = new AddMattersNewDialogFragment();
                fragment.setType(currentPosition + 1);
                fragment.show(getSupportFragmentManager());
            }
        });

        titleList = new ArrayList<>();
        fragmentList = new ArrayList<>();
//        titleList.add("全部");
        titleList.add("工作");
        titleList.add("生活");
        titleList.add("个人成长");
        titleList.add("开支消费");
        for (int i = 0; i < titleList.size(); i++) {
            final TabLayout.Tab tab = binding.tabLayout.newTab();
            tab.setCustomView(getTabView(i));
            final View tabView = (View) tab.getCustomView().getParent();
            tabView.setTag(i);
            binding.tabLayout.addTab(tab);
            MattersItemFragment fragment = new MattersItemFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("index", i + 1);
            bundle.putString("tab_title", titleList.get(i));
            fragment.setArguments(bundle);
            fragmentList.add(fragment);
        }
        DefaultViewPagerAdapter adapter = new DefaultViewPagerAdapter(getSupportFragmentManager(), titleList, fragmentList, 1);
        binding.viewpager.setAdapter(adapter);
//        binding.tabLayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(0);
        updateTabTextView(binding.tabLayout.getTabAt(0), true);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                currentPosition = tab.getPosition();
                updateTabTextView(tab, true);
                binding.viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        TextView tabSelect = tab.getCustomView().findViewById(R.id.tv_tab);
        LinearLayout container = tab.getCustomView().findViewById(R.id.container);
        tabSelect.setTextColor(isSelect ? Color.parseColor("#FFFFFFFF") : Color.parseColor("#FF333333"));
        container.setBackgroundResource(isSelect ? R.drawable.shape_tab_bg_sel : R.drawable.shape_tab_bg_nor);
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_matters, null);
        TextView textView = view.findViewById(R.id.tv_tab);
        textView.setText(titleList.get(currentPosition));
        return view;
    }
}
